﻿package mortal.game.view.friendBless.view
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.friendBless.view.data.*;
    import mortal.game.view.friendBless.view.renderer.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class FriendBlessWin extends Window
    {
        private var _friendBlessList:GTileList;
        private var _pageSelecter:PageSelecter;
        private var _exp:GTextFiled;
        private var _lingqi:GTextFiled;
        private var _blessNum:GTextFiled;
        private static var _instance:FriendBlessWin;

        public function FriendBlessWin(param1:ILayer = null)
        {
            super(param1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            _titleSpriteHight = 60;
            this.pushUIToDisposeVec(UIFactory.bg(0, 0, 765 + 9 - 120, 382 - 19, this, ImagesConst.WindowBg3));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.RankingTitleDecorate, 275 - 19 - 70, 8 - 44, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.FriendBlessTitle, 328 + 7 - 70, 8 - 44 + 55, this));
            this.pushUIToDisposeVec(UIFactory.bg(8 + 3, 39 + 11, 747 + 5 - 120, 337 + 12, this));
            this.pushUIToDisposeVec(UIFactory.bg(8, 39 + 13, 747 + 5 - 120, 23, this, ImagesConst.RegionTitleBg));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.FriendBless_FriendName, 103 - 10, 42 + 13, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.FriendBless_BlessReward, 451 - 108 - 70 + 11, 42 + 13, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.FriendBless_Operate, 640 + 9 - 120 + 16, 42 + 13, this));
            this._friendBlessList = UIFactory.tileList(12, 39 + 36, 756 - 120, 258, this);
            this._friendBlessList.columnWidth = 745 + 6 - 120;
            this._friendBlessList.rowHeight = 49;
            this._friendBlessList.horizontalGap = 0;
            this._friendBlessList.verticalGap = 0;
            this._friendBlessList.setStyle("skin", new Bitmap());
            this._friendBlessList.setStyle("cellRenderer", FriendBlessCellRenderer);
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = AlignMode.CENTER;
            this._pageSelecter = UIFactory.pageSelecter(312 - 70, 310 + 13, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.InputBg, _loc_1);
            this._pageSelecter.pageTextBoxSize = 50;
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this.pushUIToDisposeVec(UIFactory.gTextField("今日祝福收益：", 186 - 70, 338 + 13, 92, 25, this, GlobalStyle.textFormatHuang2));
            this._exp = UIFactory.gTextField("经验 999999", 271 - 70, 338 + 13, 78, 25, this, GlobalStyle.textFormatPutong);
            this._lingqi = UIFactory.gTextField("金币 999999", 347 - 70, 338 + 13, 78, 25, this, GlobalStyle.textFormatPutong);
            this.pushUIToDisposeVec(UIFactory.gTextField("今日祝福次数：", 453 - 70, 338 + 13, 92, 25, this, GlobalStyle.textFormatHuang2));
            this._blessNum = UIFactory.gTextField("15/20", 538 - 70, 338 + 13, 50, 25, this, GlobalStyle.textFormatPutong);
            this.pushUIToDisposeVec(UIFactory.gTextField("Ps：每天最多可以祝福30次，对方等级越高，奖励越丰富。", 153 + 76 - 70, 357 + 13, 518, 25, this, GlobalStyle.textFormatLv));
            this.updateAll();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.FriendBlessGetCanBlessInfo, this.onUpdateBlessListHandler);
            NetDispatcher.addCmdListener(ServerCommand.FriendBlessCongratulateInfo, this.onUpdateBlessInfoHandler);
            Dispatcher.addEventListener(EventName.FriendBlessBtnStateUpdate, this.onBlessBtnStateUpdateHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.FriendBlessGetCanBlessInfo, this.onUpdateBlessListHandler);
            NetDispatcher.removeCmdListener(ServerCommand.FriendBlessCongratulateInfo, this.onUpdateBlessInfoHandler);
            return;
        }// end function

        private function updateAll() : void
        {
            this.updatePageInfo();
            this.updateBlessList();
            this.updateBottomBlessInfo();
            return;
        }// end function

        private function onUpdateBlessListHandler(param1:Object) : void
        {
            this.updatePageInfo();
            this.updateBlessList();
            return;
        }// end function

        private function updatePageInfo() : void
        {
            var _loc_1:* = Cache.instance.friendBless.friendCongratulateList;
            this._pageSelecter.maxPage = Math.ceil(_loc_1.length / 5);
            return;
        }// end function

        private function updateBlessList() : void
        {
            var _loc_7:FriendCongratulateData = null;
            var _loc_8:SMiniPlayer = null;
            var _loc_1:* = Cache.instance.friendBless.friendCongratulateList.slice();
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                _loc_7 = _loc_1[_loc_2] as FriendCongratulateData;
                _loc_8 = Cache.instance.friend.getFriendById(_loc_7.miniPlayer.entityId.id);
                if (_loc_8 && !_loc_8.online)
                {
                    _loc_1.splice(_loc_2, 1);
                    _loc_2 = _loc_2 - 1;
                }
                _loc_2++;
            }
            var _loc_3:* = (this._pageSelecter.currentPage - 1) * 5;
            var _loc_4:* = this._pageSelecter.currentPage * 5;
            var _loc_5:* = _loc_1.slice(_loc_3, _loc_4);
            var _loc_6:* = new DataProvider();
            _loc_6.addItems(_loc_5);
            this._friendBlessList.dataProvider = _loc_6;
            this._friendBlessList.drawNow();
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = 604;
                _closeBtn.y = 5 + 19;
            }
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.updateBlessList();
            return;
        }// end function

        private function updateBottomBlessInfo() : void
        {
            var _loc_2:int = 0;
            var _loc_1:* = Cache.instance.friendBless.sCongratulateInfo;
            if (_loc_1)
            {
                this._exp.text = "经验 " + _loc_1.todayExper;
                this._lingqi.text = "金币 " + _loc_1.todayVitalEnergy;
                _loc_2 = ConstConfig.instance.getValueByName("TFriendCongratulateTimes");
                this._blessNum.text = _loc_1.todayCongratulateTimes + "/" + _loc_2;
            }
            return;
        }// end function

        private function onUpdateBlessInfoHandler(param1:Object) : void
        {
            this.updateBottomBlessInfo();
            return;
        }// end function

        private function onBlessBtnStateUpdateHandler(event:DataEvent) : void
        {
            var _loc_6:FriendCongratulateData = null;
            var _loc_2:* = Cache.instance.friendBless.friendCongratulateList.slice();
            var _loc_3:* = (this._pageSelecter.currentPage - 1) * 5;
            var _loc_4:* = this._pageSelecter.currentPage * 5;
            var _loc_5:* = _loc_2.slice(_loc_3, _loc_4);
            for each (_loc_6 in _loc_5)
            {
                
                if (!_loc_6.hasBlessed)
                {
                    return;
                }
            }
            this.hide();
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            Cache.instance.friendBless.friendCongratulateList = [];
            return;
        }// end function

        override public function stageResize() : void
        {
            x = (Global.stage.stageWidth - this.width) / 2;
            y = (Global.stage.stageHeight - this.height) / 2;
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.stageResize();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._friendBlessList.dispose();
            this._friendBlessList = null;
            this._pageSelecter.dispose();
            this._pageSelecter = null;
            this._exp.dispose();
            this._exp = null;
            this._lingqi.dispose();
            this._lingqi = null;
            this._blessNum.dispose();
            this._blessNum = null;
            this.removeListeners();
            return;
        }// end function

        public static function get instance() : FriendBlessWin
        {
            if (_instance == null)
            {
                _instance = new FriendBlessWin;
            }
            return _instance;
        }// end function

    }
}
